/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package popups;

import Enums.ProjectType;
import PCR.MainFrame;
import PCR.ShapesDialog;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author icdev
 */
public class PopupFrameManager {

    DefaultTableModel smearsModel;
    DefaultTableModel bandsModel;
    DefaultTableModel dimersModel;
    DefaultTableModel housekeepingModel;
    ToolActionCommander commander = new ToolActionCommander();
    ToolReplaceCommander replaceCommnder = new ToolReplaceCommander();
    MainFrame frm = (MainFrame) java.awt.Window.getWindows()[0];

    public PopupFrameManager() {
        bandsModel = PopupParser.getComponents("Band");
        smearsModel = PopupParser.getComponents("Smear");
        dimersModel = PopupParser.getComponents("Dimer");
        housekeepingModel = PopupParser.getComponents("HouseKeeping");

        commander.setBandsModel(bandsModel);
        commander.setSmearsModel(smearsModel);
        commander.setDimersModel(dimersModel);
        commander.setHouseKeepingsModel(housekeepingModel);


        replaceCommnder.setBandsModel(bandsModel);
        replaceCommnder.setSmearsModel(smearsModel);
        replaceCommnder.setDimersModel(dimersModel);
        replaceCommnder.setHouseKeepingModelModel(housekeepingModel);
    }

    public void getBands(Container container, boolean getAll) {
        container.removeAll();
        MainFrame frm = (MainFrame) java.awt.Window.getWindows()[0];
        int sCount = 7;
        if (getAll) {
            sCount = bandsModel.getRowCount()-1;
        }
        for (int i = 0; i < sCount; i++) {
            if (i > bandsModel.getRowCount()-1) {
                break;
            }

            if (frm.getSelectedWorkArea().getProjectType()==ProjectType.PRO) {
                String bandType = (String) bandsModel.getValueAt(i, 0);
                String bandIamge = (String) bandsModel.getValueAt(i, 1);
                JButton btn = new JButton();

                if (!ShapesDialog.isReplacer()) {
                    System.out.println("commander");
                    commander.setToolTitle("Bands");
                    btn.addMouseListener(commander);
                } else {
                    System.out.println("replacer commander");
                    replaceCommnder.setToolTitle("Bands");
                    btn.addMouseListener(replaceCommnder);
                }

                Icon icn = new javax.swing.ImageIcon("resources/proteinsBand/" + bandIamge);
                Image img = ((ImageIcon) icn).getImage();
                ImageIcon icon = new ImageIcon(img.getScaledInstance(70, 30, Image.SCALE_AREA_AVERAGING));
                btn.setIcon(icon);
                btn.getAccessibleContext().setAccessibleDescription(Integer.toString(i));
                btn.setToolTipText(bandType);
                btn.getAccessibleContext().setAccessibleName("Band");
                Dimension dim = new Dimension(50, 30);
                container.add(btn);
            } else if (frm.getSelectedWorkArea().getProjectType()==ProjectType.PCR || frm.getSelectedWorkArea().getProjectType()==ProjectType.MRNA) {

                String bandType = (String) bandsModel.getValueAt(i, 0);
                String bandIamge = (String) bandsModel.getValueAt(i, 1);
                JButton btn = new JButton();

                if (!ShapesDialog.isReplacer()) {
                    commander.setToolTitle("Bands");
                    btn.addMouseListener(commander);
                } else {
                    replaceCommnder.setToolTitle("Bands");
                    btn.addMouseListener(replaceCommnder);
                }

                Icon icn = new javax.swing.ImageIcon("resources/bands/" + bandIamge);
                Image img = ((ImageIcon) icn).getImage();
                ImageIcon icon = new ImageIcon(img.getScaledInstance(70, 30, Image.SCALE_AREA_AVERAGING));
                btn.setIcon(icon);
                btn.getAccessibleContext().setAccessibleDescription(Integer.toString(i));
                btn.setToolTipText(bandType);
                btn.getAccessibleContext().setAccessibleName("Band");
                Dimension dim = new Dimension(50, 30);
                container.add(btn);
            }
        }


        if (!getAll) {
            JButton lbl = new JButton();
            lbl.setHorizontalTextPosition(SwingConstants.CENTER);
            lbl.setHorizontalAlignment(SwingConstants.CENTER);
            lbl.setText("more...");
            lbl.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    MainFrame frm = (MainFrame) java.awt.Window.getWindows()[0];
                    ShapesDialog shd = new ShapesDialog(frm, true);
                    ShapesDialog.setReplacer(false);
                    shd.loadShapes("Band");
                    shd.setVisible(true);
                }
            });
            container.add(lbl);
        }
    }

    public void getSmears(Container container, boolean getAll) {
        container.removeAll();
        int sCount = 7;
        if (getAll) {
            sCount = smearsModel.getRowCount() - 1;
        }
        for (int i = 0; i < sCount; i++) {
            if (i > smearsModel.getRowCount() - 1) {
                break;
            }

            if (frm.getSelectedWorkArea().getProjectType().toString().equals("PRO")) {
                String smearType = (String) smearsModel.getValueAt(i, 0);
                String smearIamge = (String) smearsModel.getValueAt(i, 1);

                JButton btn = new JButton();
                if (!ShapesDialog.isReplacer()) {
                    commander.setToolTitle("Smears");
                    btn.addMouseListener(commander);
                } else {
                    replaceCommnder.setToolTitle("Smears");
                    btn.addMouseListener(replaceCommnder);
                }

                Icon icn = new javax.swing.ImageIcon("resources/proteinsmears/" + smearIamge);
                Image img = ((ImageIcon) icn).getImage();

                ImageIcon icon = new ImageIcon(img.getScaledInstance(70, 30, Image.SCALE_AREA_AVERAGING));
                btn.setIcon(icon);

                btn.getAccessibleContext().setAccessibleName("Smear");
                btn.getAccessibleContext().setAccessibleDescription(Integer.toString(i));
                btn.setToolTipText(smearType);

                Dimension dim = new Dimension(50, 30);
                container.add(btn);
            } else if (frm.getSelectedWorkArea().getProjectType().toString().equals("PCR")) {
                String smearType = (String) smearsModel.getValueAt(i, 0);
                String smearIamge = (String) smearsModel.getValueAt(i, 1);

                JButton btn = new JButton();
                if (!ShapesDialog.isReplacer()) {
                    commander.setToolTitle("Smears");
                    btn.addMouseListener(commander);
                } else {
                    replaceCommnder.setToolTitle("Smears");
                    btn.addMouseListener(replaceCommnder);
                }

                Icon icn = new javax.swing.ImageIcon("resources/smears/" + smearIamge);
                Image img = ((ImageIcon) icn).getImage();

                ImageIcon icon = new ImageIcon(img.getScaledInstance(70, 30, Image.SCALE_AREA_AVERAGING));
                btn.setIcon(icon);

                btn.getAccessibleContext().setAccessibleName("Smear");
                btn.getAccessibleContext().setAccessibleDescription(Integer.toString(i));
                btn.setToolTipText(smearType);

                Dimension dim = new Dimension(50, 30);
                container.add(btn);
            }
        }
        if (!getAll) {
            JButton lbl = new JButton();
            lbl.setHorizontalTextPosition(SwingConstants.CENTER);
            lbl.setHorizontalAlignment(SwingConstants.CENTER);
            lbl.setText("more...");
            lbl.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    MainFrame frm = (MainFrame) java.awt.Window.getWindows()[0];
                    ShapesDialog shd = new ShapesDialog(frm, true);
                    ShapesDialog.setReplacer(false);
                    shd.loadShapes("Smear");
                    shd.setVisible(true);
                }
            });
            container.add(lbl);
        }
    }

    public void getDimers(Container container, boolean getAll) {
        container.removeAll();
        int sCount = 7;
        if (getAll) {
            sCount = dimersModel.getRowCount() - 1;
        }
        for (int i = 0; i < sCount; i++) {
            if (i > dimersModel.getRowCount() - 1) {
                break;
            }
            String dimerType = (String) dimersModel.getValueAt(i, 0);
            String dimerIamge = (String) dimersModel.getValueAt(i, 1);

            JButton btn = new JButton();

            if (!ShapesDialog.isReplacer()) {
                commander.setToolTitle("Dimers");
                btn.addMouseListener(commander);
            } else {
                replaceCommnder.setToolTitle("Dimers");
                btn.addMouseListener(replaceCommnder);
            }
            Icon icn = new javax.swing.ImageIcon("resources/dimers/" + dimerIamge);
            Image img = ((ImageIcon) icn).getImage();

            ImageIcon icon = new ImageIcon(img.getScaledInstance(70, 30, Image.SCALE_AREA_AVERAGING));
            btn.setIcon(icon);
            btn.getAccessibleContext().setAccessibleDescription(Integer.toString(i));
            btn.setToolTipText(dimerType);
            btn.getAccessibleContext().setAccessibleName("Dimer");
            Dimension dim = new Dimension(50, 30);
            container.add(btn);
        }
        if (!getAll) {
            JButton lbl = new JButton();
            lbl.setHorizontalTextPosition(SwingConstants.CENTER);
            lbl.setHorizontalAlignment(SwingConstants.CENTER);
            lbl.setText("more...");
            lbl.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    MainFrame frm = (MainFrame) java.awt.Window.getWindows()[0];
                    ShapesDialog shd = new ShapesDialog(frm, true);
                    ShapesDialog.setReplacer(false);
                    shd.loadShapes("Dimer");
                    shd.setVisible(true);
                }
            });
            container.add(lbl);
        }
    }

    public void getHouseKeepings(Container container, boolean getAll) {
        container.removeAll();
        int sCount = 7;
        if (getAll) {
            sCount = housekeepingModel.getRowCount() - 1;
        }
        for (int i = 0; i < sCount; i++) {
            if (i > housekeepingModel.getRowCount() - 1) {
                break;
            }
            String dimerType = (String) housekeepingModel.getValueAt(i, 0);
            String dimerIamge = (String) housekeepingModel.getValueAt(i, 1);

            JButton btn = new JButton();

            if (!ShapesDialog.isReplacer()) {
                commander.setToolTitle("HouseKeepings");
                btn.addMouseListener(commander);
            } else {
                replaceCommnder.setToolTitle("HouseKeepings");
                btn.addMouseListener(replaceCommnder);
            }
            Icon icn = new javax.swing.ImageIcon("resources/housekeepings/" + dimerIamge);
            Image img = ((ImageIcon) icn).getImage();

            ImageIcon icon = new ImageIcon(img.getScaledInstance(70, 30, Image.SCALE_AREA_AVERAGING));
            btn.setIcon(icon);
            btn.getAccessibleContext().setAccessibleDescription(Integer.toString(i));
            btn.setToolTipText(dimerType);
            btn.getAccessibleContext().setAccessibleName("HouseKeeping");
            Dimension dim = new Dimension(50, 30);
            container.add(btn);
        }
        if (!getAll) {
            JButton lbl = new JButton();
            lbl.setHorizontalTextPosition(SwingConstants.CENTER);
            lbl.setHorizontalAlignment(SwingConstants.CENTER);
            lbl.setText("more...");
            lbl.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    MainFrame frm = (MainFrame) java.awt.Window.getWindows()[0];
                    ShapesDialog shd = new ShapesDialog(frm, true);
                    ShapesDialog.setReplacer(false);
                    shd.loadShapes("HouseKeeping");
                    shd.setVisible(true);
                }
            });
            container.add(lbl);
        }
    }

    public DefaultTableModel getBandsModel() {
        return bandsModel;
    }

    public DefaultTableModel getSmearsModel() {
        return smearsModel;
    }

    public DefaultTableModel getDimersModel() {
        return dimersModel;
    }

    public DefaultTableModel getHouseKeepingsModel() {
        return housekeepingModel;
    }
}
